//
//  CalendarExtensions.swift
//  hello
//
//  Created by 赵康 on 2024/12/23.
//

import Foundation

extension Calendar {
	static var mondayFirst: Calendar {
		var calendar = Calendar.current
		calendar.firstWeekday = 2  // 2 代表周一
		return calendar
	}
	
	func startOfMonth(for date: Date) -> Date {
		return self.date(from: self.dateComponents([.year, .month], from: date))!
	}
	
	func endOfMonth(for date: Date) -> Date {
		guard let nextMonth = self.date(byAdding: .month, value: 1, to: self.startOfMonth(for: date)) else {
			return date
		}
		return nextMonth
	}
}
extension Date {
	func startOfDay() -> Date {
		return Calendar.current.startOfDay(for: self)
	}
	
	func endOfDay() -> Date {
		let nextDay = Calendar.current.date(byAdding: .day, value: 1, to: self.startOfDay())!
		return nextDay
	}
}
extension DateFormatter {
	static let yyyyMMdd: DateFormatter = {
		let formatter = DateFormatter()
		formatter.dateFormat = "yyyyMMdd"
		formatter.timeZone = TimeZone.current
		formatter.calendar = Calendar.current
		return formatter
	}()
	
	static let yyyyMM: DateFormatter = {
		let formatter = DateFormatter()
		formatter.dateFormat = "yyyyMM"
		formatter.timeZone = TimeZone.current
		formatter.calendar = Calendar.current
		return formatter
	}()
}
